//
//  ViewController.m
//  AnimationDemo
//
//  Created by zhangjikuan on 2020/5/20.
//  Copyright © 2020 hsgd. All rights reserved.
//

#define kColor_Top [UIColor colorWithRed:34/255.0 green:44/255.0 blue:87/255.0 alpha:1]
#define kColor_Bottom [UIColor colorWithRed:223/255.0 green:157/255.0 blue:16/255.0 alpha:1]
#define kColor_Background [UIColor colorWithRed:225/255.0 green:224/255.0 blue:233/255.0 alpha:1]


#import "ViewController.h"
#import "SortC.h"
#import "YaoFactory.h"
#import "YangYaoFactory.h"
#import "YangYao.h"

@interface LineProgressView : UIView

/// 进度条颜色
@property (nonatomic, strong) UIColor *strokeColor;

/// 是否是渐变色
@property (nonatomic, assign) BOOL gradual;

/// 进度值 【0，1】的数值范围
@property (nonatomic, assign) CGFloat progress;
- (void)setProgress:(CGFloat)progress;

@property (strong, nonatomic) CAShapeLayer *frontShapeLayer;
@property (strong, nonatomic) CAShapeLayer *backShapeLayer;
@property (strong, nonatomic) UIBezierPath *lineBezierPath;
@property (strong, nonatomic) UIBezierPath *frontBezierPath;
/// 渐变用
@property (nonatomic, strong) CAGradientLayer *rightGradLayer;
@property (nonatomic, strong) CAGradientLayer *leftGradLayer;
@property (nonatomic, strong) CAGradientLayer *frontGradLayer;

@property (nonatomic, strong) CALayer *gradLayer;

@end

@implementation LineProgressView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
    }
    return self;
}

-(void)drawRect:(CGRect)rect{

    CGFloat kWidth = rect.size.width;
    CGFloat kHeight = rect.size.height;
    
    if (!self.lineBezierPath){
        self.lineBezierPath = ({
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:CGPointMake(0,0)];
            [path addLineToPoint:CGPointMake(self.bounds.size.width,0)];
            path;
        });
    }
    if (!self.frontBezierPath) {
        self.frontBezierPath = ({
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:CGPointMake(0,0)];
            [path addLineToPoint:CGPointMake(self.bounds.size.width,0)];
            path;
        });
    }
    if (!self.backShapeLayer) {
        self.backShapeLayer = ({
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            shapeLayer.frame = rect;
            shapeLayer.path = self.lineBezierPath.CGPath;
            shapeLayer.fillColor = [UIColor blueColor].CGColor;
            shapeLayer.lineWidth = 0;
            shapeLayer.strokeColor = [UIColor colorWithRed:23.00/255.00
                                                     green:144.00/255.00
                                                      blue:255.00/255.00
                                                     alpha:0.2].CGColor;
            shapeLayer.lineCap = kCALineCapRound;
            [self.layer addSublayer:shapeLayer];
            shapeLayer;
        });
    }

    if (!self.frontShapeLayer){
        self.frontShapeLayer = ({
            CAShapeLayer  *shapeLayer = [CAShapeLayer layer];
            shapeLayer.frame = rect;
            shapeLayer.fillColor = [UIColor colorWithRed:255/255.0 green:155/255.0 blue:96/255.0 alpha:1.0].CGColor;
            shapeLayer.lineWidth = 0.0;
           
            if (self.strokeColor) {
                shapeLayer.strokeColor = self.strokeColor.CGColor;
            } else {
                shapeLayer.strokeColor = [UIColor colorWithRed:23.00/255.00
                                                                    green:144.00/255.00
                                                                     blue:255.00/255.00
                                                                    alpha:1].CGColor;
            }
            shapeLayer;
        });
        if (self.gradual) {
            [self addGradLayerWithRect:rect];
            self.frontShapeLayer.lineCap = kCALineCapRound;
            _gradLayer.mask = self.frontShapeLayer;
            [self.layer addSublayer:self.frontShapeLayer];
        }else{
            self.frontShapeLayer.lineCap = kCALineCapRound;
            [self.layer addSublayer:self.frontShapeLayer];
        }
    }

    [self startAnimationValue:self.progress];
}
- (void)addGradLayerWithRect:(CGRect)rect{
    _frontGradLayer = ({
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = self.frontShapeLayer.bounds;
        gl.cornerRadius = self.bounds.size.height*0.5;
        gl.masksToBounds = YES;
        gl.startPoint = CGPointMake(0, 0.5);
        gl.endPoint = CGPointMake(1, 0.53);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:106/255.0 blue:79/255.0 alpha:1.0].CGColor,
                      (__bridge id)[UIColor colorWithRed:255/255.0 green:155/255.0 blue:96/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(0.9f)];
        gl;
    });
    
   
    [self.frontShapeLayer addSublayer:_frontGradLayer];
}
- (void)startAnimationValue:(CGFloat)value{
    
    [self.frontBezierPath removeAllPoints];
    
    [self.frontBezierPath moveToPoint:CGPointMake(5, 0)];
    [self.frontBezierPath addLineToPoint:CGPointMake(value * self.bounds.size.width , 0)];
    [self.frontBezierPath addLineToPoint:CGPointMake(value * self.bounds.size.width + 5, self.bounds.size.height*0.5)];
    [self.frontBezierPath addLineToPoint:CGPointMake(value *self.bounds.size.width, self.bounds.size.height)];
    [self.frontBezierPath addLineToPoint:CGPointMake(5, self.bounds.size.height)];
    [self.frontBezierPath addQuadCurveToPoint:CGPointMake(5, 0) controlPoint:CGPointMake(0, self.bounds.size.height * 0.5)];
    
    self.frontGradLayer.frame = CGRectMake(0, 0, value * self.bounds.size.width , self.bounds.size.height);
    [self.frontGradLayer removeFromSuperlayer];
    self.frontShapeLayer.masksToBounds = YES;
    [self.frontShapeLayer addSublayer:self.frontGradLayer];
    self.frontShapeLayer.path = self.frontBezierPath.CGPath;
    
    [self.lineBezierPath removeAllPoints];
    
    [self.lineBezierPath moveToPoint:CGPointMake(value * self.bounds.size.width + 2, 0)];
    [self.lineBezierPath addLineToPoint:CGPointMake(value * self.bounds.size.width + 5 + 2, self.bounds.size.height*0.5)];
    [self.lineBezierPath addLineToPoint:CGPointMake(value * self.bounds.size.width + 2, self.bounds.size.height)];
    [self.lineBezierPath addLineToPoint:CGPointMake(self.bounds.size.width - 5, self.bounds.size.height)];
    [self.lineBezierPath addQuadCurveToPoint:CGPointMake(self.bounds.size.width - 5, 0)
                                controlPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height*0.5)];
    [self.lineBezierPath addLineToPoint:CGPointMake(value * self.bounds.size.width + 1, 0)];
    
    self.backShapeLayer.path = self.lineBezierPath.CGPath;
    

    CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnima.duration = 1.0f;
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pathAnima.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnima.toValue = [NSNumber numberWithFloat:1];
    pathAnima.fillMode = kCAFillModeForwards;
    pathAnima.removedOnCompletion = NO;
    [self.frontShapeLayer addAnimation:pathAnima forKey:@"strokeEndAnimation"];
}
- (void)setStrokeColor:(UIColor *)strokeColor {
    _strokeColor = strokeColor;
}
- (void)setGradual:(BOOL)gradual{
    _gradual = gradual;
    if (gradual) {
        [self.frontShapeLayer removeFromSuperlayer];
        self.frontShapeLayer = nil;
    }else{
        [_gradLayer removeFromSuperlayer];
        _gradLayer = nil;
        [self.frontShapeLayer removeFromSuperlayer];
        self.frontShapeLayer = nil;
    }
}
- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    
    
    [self setNeedsDisplay];
}


@end
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    LineProgressView *progress = [[LineProgressView alloc] initWithFrame:CGRectMake(5, 100, self.view.bounds.size.width-10, 10)];
    progress.gradual = YES;
    progress.strokeColor = [UIColor clearColor];
    [self.view addSubview:progress];

    [progress setProgress:0.5];


    
    UIView *yaoView = [[UIView alloc] init];
    yaoView.frame = CGRectMake(15, 120, 60, 60);
    yaoView.backgroundColor = UIColor.blackColor;
    
    CAShapeLayer *layer = [self yaoLayer];
    [yaoView.layer addSublayer:layer];
    
    Yao *yangyao = [[[YangYaoFactory alloc] init] getYao];
    
    CAShapeLayer *layer2 = yangyao.yaoLayer;
    
    layer2.frame = CGRectMake(15,25,30,5);

    [yaoView.layer addSublayer:layer2];
    
    ///
    NSInteger yaoNumber = 6;
    /// first yao Y
    CGFloat yaoH = yaoView.bounds.size.height/yaoNumber;
    CGFloat firstYaoY = yaoH * 0.5;
    CGFloat width = yaoView.bounds.size.width * 0.5;
    CGFloat yaoX = width *0.5;
    
//
//    for (NSInteger i = 0; i < yaoNumber; i ++) {
//
//        CAShapeLayer *layer = [self yaoLayer];
//
//        layer.frame = CGRectMake(yaoX, firstYaoY + i * yaoH, width, 5);
//
//        [yaoView.layer addSublayer:layer];
//    }
    
    [self.view addSubview:yaoView];
    
//    [SortC test];
    
//    for (int i = 0; i < 300000; i ++) {
//        NSLog(@"index == %ld", i);
//        SortC *sort = [[SortC alloc] init];
//
//        [sort test];
//    }
    
    
    /// 六十四
    /// 8*2 = 8 x 8 = 62
    /// 2 *3 x 2*3 =
    /// 2*3 = 8
    /// 2*5 = 32
    /// 2*6 = 2*3 x  2*3 = 8 x 8 = 64
    /// 2*7 = 128
    /// 2*8 = 256
    /// 2*9 = 512
    /// 2*10 = 1024
    
    /// 8 * 8
    /// 计算机系统是二进制代码表示两种情况，0 1 yes no true false  一个位置两种意思，表示两种状态。 0 表示正确 1表示错误
    ///  乾 坤 泽 艮 巽 震 坎 离
    
    
}

- (CAShapeLayer *)yaoLayer {
    
    /// 平均分为三分
    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointMake(0,0)];
//    [path addLineToPoint:CGPointMake(30,0)];
//
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(15, 15, 30, 5);
    layer.path = path.CGPath;
    layer.fillColor = [UIColor blueColor].CGColor;
    layer.lineWidth = 5;
    layer.borderWidth = 0.1;
    layer.strokeEnd = 1.0;
    layer.strokeColor = UIColor.clearColor.CGColor;
    layer.strokeStart = 0.0;
    
    CAShapeLayer *layer1 = [CAShapeLayer layer];
    layer1.frame = CGRectMake(0, 0, 14, 5);
    [path moveToPoint:CGPointMake(0,0)];
    [path addLineToPoint:CGPointMake(14,0)];
    layer1.path = path.CGPath;
    layer1.lineWidth = 5;
    layer1.borderWidth = 0.1;
    layer1.strokeEnd = 0.3;
    layer1.strokeColor = UIColor.whiteColor.CGColor;
    layer1.strokeStart = 0.0;
    
    CAShapeLayer *layer2 = [CAShapeLayer layer];
    layer2.frame = CGRectMake(16, 0, 14, 5);
    
    [path moveToPoint:CGPointMake(0,0)];
    [path addLineToPoint:CGPointMake(14,0)];
    layer2.path = path.CGPath;
    layer2.fillColor = [UIColor blueColor].CGColor;
    layer2.lineWidth = 5;
    layer2.borderWidth = 0.1;
    layer2.strokeEnd = 1.0;
    layer2.strokeColor = UIColor.whiteColor.CGColor;
    layer2.strokeStart = 0.5;
    [layer addSublayer:layer1];
    [layer addSublayer:layer2];
    
    CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnima.duration = 1.5f;
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pathAnima.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnima.toValue = [NSNumber numberWithFloat:1.0];
    pathAnima.fillMode = kCAFillModeForwards;
    pathAnima.removedOnCompletion = NO;
//    [layer addAnimation:pathAnima forKey:@"strokeEndAnimation"];
    
    [layer1 addAnimation:pathAnima forKey:@"strokeEndAnimation"];
    
    [layer2 addAnimation:pathAnima forKey:@"strokeEndAnimation"];

    return layer;
}
@end
